package com.lanou.zy.projecta.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.database.DBHelper;
import com.lanou.zy.projecta.greedao.entity.User;
import com.lanou.zy.projecta.search.ClearEditText;
import com.lanou.zy.projecta.util.CheckEditText;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/8.
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private Button registerBtn;
    private ClearEditText registerName, registerPsw;
    private String name;
    private String psw;
    private ImageView backToUserIv;

    private DBHelper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        initData();
    }

    private void initData() {

    }

    private void initView() {
        registerName = (ClearEditText) findViewById(R.id.registerName);
        registerPsw = (ClearEditText) findViewById(R.id.registerPsw);
        registerPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
        backToUserIv = (ImageView) findViewById(R.id. backToUserIv);
        backToUserIv.setOnClickListener(this);

        registerBtn = (Button) findViewById(R.id.registerBtn);
        registerBtn.setOnClickListener(this);
        helper = DBHelper.getInstance();
    }

//    /**
//     * 显示Toast消息
//     *
//     * @param msg
//     */
//    private void showToast(String msg) {
//        if (toast == null) {
//            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
//        } else {
//            toast.setText(msg);
//        }
//        toast.show();
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.registerBtn:
                if (checkEditText()) {
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.backToUserIv:
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    /**
     * 检查输入是否规范
     */
    private boolean checkEditText() {
        name = registerName.getText().toString().trim();
        psw = registerPsw.getText().toString().trim();

        if (CheckEditText.isMobile(name) || CheckEditText.isEmail(name)) {
            if (CheckEditText.isPsw(psw)) {
                if (helper.isUserSaves(name)) {
                    Toast.makeText(this, "已存在的用户名!", Toast.LENGTH_SHORT).show();
                    return false;
                } else {
                    List<User> users = new ArrayList<>();
                    User user = new User(name, psw);
                    user.setUserName(name);
                    user.setUserPsw(psw);
                    users.add(user);
                    helper.addUser(users);
                    Toast.makeText(this, "注册成功\n请牢记账号" + name + "\n密码" + psw, Toast.LENGTH_SHORT).show();
                    return true;
                }
            } else {
                Toast.makeText(this, "密码格式不正确，请输入6~12位由字母和数字组成的密码", Toast.LENGTH_SHORT).show();
                return false;
            }
        } else {
            Toast.makeText(this, "账号格式不正确，请输入手机号或者邮箱", Toast.LENGTH_SHORT).show();
            return false;
        }

    }

}
